package com.scms.secondclass.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.scms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scms.secondclass.mapper.ActivityCategoryMapper;
import com.scms.secondclass.domain.ActivityCategory;
import com.scms.secondclass.service.IActivityCategoryService;

/**
 * 活动分类Service业务层处理
 * 
 * @author thy
 * @date 2025-03-06
 */
@Service
public class ActivityCategoryServiceImpl implements IActivityCategoryService 
{
    @Autowired
    private ActivityCategoryMapper activityCategoryMapper;

    /**
     * 查询活动分类
     * 
     * @param categoryId 活动分类主键
     * @return 活动分类
     */
    @Override
    public ActivityCategory selectActivityCategoryByCategoryId(Long categoryId)
    {
        return activityCategoryMapper.selectActivityCategoryByCategoryId(categoryId);
    }

    /**
     * 查询活动分类列表
     * 
     * @param activityCategory 活动分类
     * @return 活动分类
     */
    @Override
    public List<ActivityCategory> selectActivityCategoryList(ActivityCategory activityCategory)
    {
        return activityCategoryMapper.selectActivityCategoryList(activityCategory);
    }

    /**
     * 查询活动分类树形结构列表
     * 
     * @param activityCategory 活动分类
     * @return 活动分类树形结构集合
     */
    @Override
    public List<ActivityCategory> selectActivityCategoryTreeList(ActivityCategory activityCategory)
    {
        List<ActivityCategory> categoryList = activityCategoryMapper.selectActivityCategoryList(activityCategory);
        return buildCategoryTree(categoryList);
    }

    /**
     * 构建活动分类树形结构
     * 
     * @param categoryList 活动分类列表
     * @return 树形结构列表
     */
    private List<ActivityCategory> buildCategoryTree(List<ActivityCategory> categoryList)
    {
        List<ActivityCategory> returnList = new ArrayList<ActivityCategory>();
        List<Long> tempList = new ArrayList<Long>();
        
        // 获取所有分类ID
        for (ActivityCategory category : categoryList)
        {
            tempList.add(category.getCategoryId());
        }
        
        // 构建树形结构
        for (Iterator<ActivityCategory> iterator = categoryList.iterator(); iterator.hasNext();)
        {
            ActivityCategory category = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categoryList, category);
                returnList.add(category);
            }
        }
        
        // 如果没有顶级节点，直接返回所有分类
        if (returnList.isEmpty())
        {
            returnList = categoryList;
        }
        
        return returnList;
    }
    
    /**
     * 递归列表
     * 
     * @param list 分类列表
     * @param t 当前分类
     */
    private void recursionFn(List<ActivityCategory> list, ActivityCategory t)
    {
        // 得到子节点列表
        List<ActivityCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ActivityCategory tChild : childList)
        {
            // 判断是否有子节点
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    
    /**
     * 得到子节点列表
     * 
     * @param list 分类列表
     * @param t 当前分类
     * @return 子节点列表
     */
    private List<ActivityCategory> getChildList(List<ActivityCategory> list, ActivityCategory t)
    {
        List<ActivityCategory> tlist = new ArrayList<ActivityCategory>();
        for (ActivityCategory n : list)
        {
            if (n.getParentId() != null && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    
    /**
     * 判断是否有子节点
     * 
     * @param list 分类列表
     * @param t 当前分类
     * @return 是否有子节点
     */
    private boolean hasChild(List<ActivityCategory> list, ActivityCategory t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 新增活动分类
     * 
     * @param activityCategory 活动分类
     * @return 结果
     */
    @Override
    public int insertActivityCategory(ActivityCategory activityCategory)
    {
        activityCategory.setCreateTime(DateUtils.getNowDate());
        return activityCategoryMapper.insertActivityCategory(activityCategory);
    }

    /**
     * 修改活动分类
     * 
     * @param activityCategory 活动分类
     * @return 结果
     */
    @Override
    public int updateActivityCategory(ActivityCategory activityCategory)
    {
        activityCategory.setUpdateTime(DateUtils.getNowDate());
        return activityCategoryMapper.updateActivityCategory(activityCategory);
    }

    /**
     * 批量删除活动分类
     * 
     * @param categoryIds 需要删除的活动分类主键
     * @return 结果
     */
    @Override
    public int deleteActivityCategoryByCategoryIds(Long[] categoryIds)
    {
        return activityCategoryMapper.deleteActivityCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除活动分类信息
     * 
     * @param categoryId 活动分类主键
     * @return 结果
     */
    @Override
    public int deleteActivityCategoryByCategoryId(Long categoryId)
    {
        return activityCategoryMapper.deleteActivityCategoryByCategoryId(categoryId);
    }
}
